<?php
/**
 * @link http://www.tintsoft.com/
 * @copyright Copyright (c) 2013 TintSoft LLC
 * @license http://www.tintsoft.com/license/
 */
namespace Leaps\Base;
use Leaps;
/**
 * 请求基类
 *
 * @author Tongle Xu <xutongle@gmail.com>
 * @since 5.0
 */
abstract class Request extends Object
{
    private $_scriptFile;
    private $_isConsoleRequest;

    /**
     * 解析当前请求到一个路由和相关的参数。
     * @return array 
     */
    abstract public function resolve();

    /**
     * 返回当前是否通过命令行请求
     * @return boolean 
    */
    public function getIsConsoleRequest()
    {
        return $this->_isConsoleRequest !== null ? $this->_isConsoleRequest : PHP_SAPI === 'cli';
    }

    /**
     * 设置当前是否通过命令行请求的
     * @param boolean $value 
     */
    public function setIsConsoleRequest($value)
    {
        $this->_isConsoleRequest = $value;
    }

    /**
     * 返回输入脚本文件路径
     * @return string entry script file path (processed w/ realpath())
     * @throws InvalidConfigException if the entry script file path cannot be determined automatically.
     */
    public function getScriptFile()
    {
        if ($this->_scriptFile === null) {
            if (isset($_SERVER['SCRIPT_FILENAME'])) {
                $this->setScriptFile($_SERVER['SCRIPT_FILENAME']);
            } else {
                throw new InvalidConfigException('Unable to determine the entry script file path.');
            }
        }
        return $this->_scriptFile;
    }

    /**
     * 设置输入脚本文件路径
     * @param string $value 输入脚本文件路径。可以是一个文件路径或路径别名。
     * @throws InvalidConfigException if the provided entry script file path is invalid.
     */
    public function setScriptFile($value)
    {
        $scriptFile = realpath(Leaps::getAlias($value));
        if ($scriptFile !== false && is_file($scriptFile)) {
            $this->_scriptFile = $scriptFile;
        } else {
            throw new InvalidConfigException('Unable to determine the entry script file path.');
        }
    }
}
